package com.mingyizhudao.app.view.doctor;

import java.util.HashMap;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.dixiang.framework.common.QJNetUICallback;
import com.dixiang.framework.common.QjResult;
import com.dixiang.framework.http.DataHandler;
import com.dixiang.framework.network.Net;
import com.dixiang.framework.utils.Utils;
import com.dixiang.framework.widget.LoadingDialog;
import com.google.gson.reflect.TypeToken;
import com.mingyizhudao.app.R;
import com.mingyizhudao.app.utils.CommonUtils;
import com.mingyizhudao.app.view.MYBaseActivity;
import com.mingyizhudao.app.view.doctor.bean.DoctorDetailsEntity;
import com.mingyizhudao.app.view.doctor.bean.DoctorsEntity;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.shareboard.SnsPlatform;
import com.umeng.socialize.utils.ShareBoardlistener;

public class DoctorDetailsActivity extends MYBaseActivity {

	private ImageView iv_doctor;
	private TextView tv_name, tv_atitle, tv_dept, tv_mtitle, tv_hospital,
			tv_good_at, tv_career_exp, tv_sign, tv_tag_qianyue, tv_tag_yizhen;

	private LinearLayout ll_reason, ll_good_at, ll_honour, ll_honour1,
			ll_career_exp;
	private RelativeLayout rl_reason;
	private View btn_view;
	private DoctorDetailsEntity detailsEntity;
	private final String detailUrl = "http://m.mingyizhudao.com/mobile/doctor/view/id/";
	private String doctorId = "";
	private boolean is_commonweal = false;

	final SHARE_MEDIA[] displaylist = new SHARE_MEDIA[] { SHARE_MEDIA.WEIXIN,
			SHARE_MEDIA.WEIXIN_CIRCLE,// SHARE_MEDIA.SINA,
			SHARE_MEDIA.QQ, SHARE_MEDIA.QZONE };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_doctor_details);
		initView();
		getDoctorDetails();
	}

	@Override
	public void initHeaderView() {
		setTitleViewValue(0, 0, R.color.white);
		// setTitleViewValue("医生详情");
		setTopViewBg(R.drawable.doctor_details_top_bg);
		initBackView(R.drawable.left_back_white_icon, 0, new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});

		initOptionView(R.drawable.share, 0, new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				new ShareAction(activity).setDisplayList(displaylist)
						.withText("名医主刀")
						.setShareboardclickCallback(shareBoardlistener).open();
			}
		});
	}

	private ShareBoardlistener shareBoardlistener = new ShareBoardlistener() {

		@Override
		public void onclick(SnsPlatform arg0, SHARE_MEDIA arg1) {

			if (detailsEntity != null) {
				UMImage image = null;
				if (detailsEntity.getImageUrl() != null) {
					image = new UMImage(activity, detailsEntity.getImageUrl());
				}

				String url = detailUrl + detailsEntity.getId();
				String dsc = detailsEntity.getName() + " "
						+ detailsEntity.getaTitle() + " "
						+ detailsEntity.getHospitalName();
				String context = "国内顶尖外科手术专家";
				String descrption = detailsEntity.getDescription();
				if (Utils.isStringEmpty(descrption)) {
					String careerExp = detailsEntity.getCareerExp();
					if (!Utils.isStringEmpty(careerExp)) {
						context = careerExp;
					}
				} else {
					context = descrption;
				}

				new ShareAction(activity).setPlatform(arg1)
						.setCallback(umShareListener).withText(context)
						.withTitle(dsc).withMedia(image).withTargetUrl(url)
						.share();
			}

		}
	};

	private UMShareListener umShareListener = new UMShareListener() {
		@Override
		public void onResult(SHARE_MEDIA platform) {
			Toast.makeText(activity, platform + " 发送成功！", Toast.LENGTH_SHORT)
					.show();
		}

		@Override
		public void onError(SHARE_MEDIA platform, Throwable t) {
			Toast.makeText(activity, platform + " 发送失败！", Toast.LENGTH_SHORT)
					.show();
		}

		@Override
		public void onCancel(SHARE_MEDIA platform) {
			Toast.makeText(activity, platform + " 发送取消！", Toast.LENGTH_SHORT)
					.show();
		}
	};

	private void initView() {

		DoctorsEntity doctorListEntity = (DoctorsEntity) getIntent()
				.getSerializableExtra("doctorListEntity");
		doctorId = getIntent().getStringExtra("doctorId");
		
		if(!Utils.isStringEmpty(doctorId)){
			is_commonweal = true;
		}

		iv_doctor = (ImageView) findViewById(R.id.iv_doctor);
		tv_name = (TextView) findViewById(R.id.tv_name);
		tv_atitle = (TextView) findViewById(R.id.tv_atitle);
		tv_dept = (TextView) findViewById(R.id.tv_dept);
		tv_mtitle = (TextView) findViewById(R.id.tv_mtitle);
		tv_hospital = (TextView) findViewById(R.id.tv_hospital);
		tv_good_at = (TextView) findViewById(R.id.tv_good_at);
		tv_career_exp = (TextView) findViewById(R.id.tv_career_exp);
		tv_sign = (TextView) findViewById(R.id.tv_sign);

		ll_reason = (LinearLayout) findViewById(R.id.ll_reason);
		ll_good_at = (LinearLayout) findViewById(R.id.ll_good_at);
		ll_honour = (LinearLayout) findViewById(R.id.ll_honour);
		ll_honour1 = (LinearLayout) findViewById(R.id.ll_honour1);
		ll_career_exp = (LinearLayout) findViewById(R.id.ll_career_exp);
		rl_reason = (RelativeLayout) findViewById(R.id.rl_reason);

		tv_tag_qianyue = (TextView) findViewById(R.id.tv_tag_qianyue);
		tv_tag_yizhen = (TextView) findViewById(R.id.tv_tag_yizhen);
		tv_tag_qianyue.setVisibility(View.GONE);
		tv_tag_yizhen.setVisibility(View.GONE);

		rl_reason.setVisibility(View.GONE);
		ll_good_at.setVisibility(View.GONE);
		// ll_honour.setVisibility(View.GONE);
		ll_honour1.setVisibility(View.GONE);
		ll_career_exp.setVisibility(View.GONE);

		btn_view = findViewById(R.id.btn_view);

		initHeader(doctorListEntity);

	}

	private void initHeader(final DoctorsEntity doctorListEntity) {
		if (doctorListEntity == null) {
			return;
		}

		doctorId = doctorListEntity.getId();
		if ("1".equals(doctorListEntity.getIsContracted())) {
			tv_tag_qianyue.setVisibility(View.VISIBLE);
			btn_view.setVisibility(View.VISIBLE);
			tv_sign.setText("签约专家");

			findViewById(R.id.btn_order).setOnClickListener(
					new OnClickListener() {
						@Override
						public void onClick(View v) {
							CommonUtils.goToCreateOrder(activity,
									doctorListEntity.getId(),
									doctorListEntity.getName(),
									doctorListEntity.getHpDeptName(),
									doctorListEntity.getHpName(),
									doctorListEntity.getActionUrl(), false);
						}
					});

		} else {
			tv_sign.setText("未签约");
		}

		if ("1".equals(doctorListEntity.getIsFreeClinic())) {
			tv_tag_yizhen.setVisibility(View.VISIBLE);
		} else {
			tv_tag_yizhen.setVisibility(View.GONE);
		}

		iv_doctor.setImageResource(R.drawable.default_doctor);
		String tempUrl = doctorListEntity.getImageUrl();
		if (tempUrl != null && tempUrl.length() > 0) {
			Net.displayImage(tempUrl, iv_doctor, R.drawable.default_doctor);
		}

		tv_name.setText(doctorListEntity.getName());
		String aTitle = doctorListEntity.getaTitle();
		if (aTitle != null && (!"null".equals(aTitle)) && (!"无".equals(aTitle))) {

			tv_atitle.setText(aTitle);
			setTitleViewValue(doctorListEntity.getName() + aTitle);
		} else {

			tv_atitle.setText("");
			setTitleViewValue(doctorListEntity.getName());
		}

		tv_dept.setText(doctorListEntity.getHpDeptName());

		String mTitle = doctorListEntity.getmTitle();
		if (mTitle != null && (!"null".equals(mTitle))) {
			tv_mtitle.setText(mTitle);
		}

		tv_hospital.setText(doctorListEntity.getHpName());
	}

	private void getDoctorDetails() {
		String actionUrl = CommonUtils.getDoctorUrl(activity) + doctorId;
		LoadingDialog.getInstance(activity).show();
		QJNetUICallback callback = new QJNetUICallback<QjResult<HashMap<String, DoctorDetailsEntity>>>(
				activity) {
			@Override
			public void onSuccess(
					QjResult<HashMap<String, DoctorDetailsEntity>> result) {

				if (result != null) {
					setDoctorDetails(result.getResults().get("doctor"));
				}
			}

			public void onError(Exception e,
					QjResult<HashMap<String, DoctorDetailsEntity>> result) {
				super.onError(e, result);
			};

			public void onCompleted(Exception e,
					QjResult<HashMap<String, DoctorDetailsEntity>> result) {
				super.onCompleted(e, result);
			};
		};
		String path = new DataHandler()
				.getDataObject(
						activity,
						actionUrl,
						null,
						new TypeToken<QjResult<HashMap<String, DoctorDetailsEntity>>>() {
						}, callback);

	}

	private void setDoctorDetails(DoctorDetailsEntity doctorDetailsEntity) {
		DoctorsEntity doctorListEntity = new DoctorsEntity();
		doctorListEntity.setId(doctorDetailsEntity.getId());
		doctorListEntity.setIsContracted(doctorDetailsEntity.getIsContracted());
		doctorListEntity.setName(doctorDetailsEntity.getName());
		doctorListEntity.setHpDeptName(doctorDetailsEntity.getHpDeptName());
		doctorListEntity.setHpName(doctorDetailsEntity.getHospitalName());
		doctorListEntity.setActionUrl(CommonUtils.getBookingUrl(activity));
		doctorListEntity.setImageUrl(doctorDetailsEntity.getImageUrl());
		doctorListEntity.setaTitle(doctorDetailsEntity.getaTitle());

		initHeader(doctorListEntity);

		detailsEntity = doctorDetailsEntity;
		iv_doctor.setImageResource(R.drawable.default_doctor);
		String tempUrl = doctorDetailsEntity.getImageUrl();
		if (tempUrl != null && tempUrl.length() > 0) {
			Net.displayImage(tempUrl, iv_doctor, R.drawable.default_doctor);
		}

		tv_name.setText(doctorDetailsEntity.getName());

		String aTitle = doctorDetailsEntity.getaTitle();
		if (aTitle != null && (!"null".equals(aTitle))) {
			tv_atitle.setText(aTitle);
		}
		tv_dept.setText(doctorDetailsEntity.getHpDeptName());

		String mTitle = doctorDetailsEntity.getmTitle();
		if (mTitle != null && (!"null".equals(mTitle))) {
			tv_mtitle.setText(mTitle);
		}

		tv_hospital.setText(doctorDetailsEntity.getHospitalName());

		String description = doctorDetailsEntity.getDescription();
		if (description != null && (!"null".equals(description))) {
			ll_good_at.setVisibility(View.VISIBLE);
			tv_good_at.setText(description);
		} else {
			ll_good_at.setVisibility(View.GONE);
		}

		String vareerExp = doctorDetailsEntity.getCareerExp();
		if (vareerExp != null && (!"null".equals(vareerExp))) {
			ll_career_exp.setVisibility(View.VISIBLE);
			tv_career_exp.setText(vareerExp);
		} else {
			ll_career_exp.setVisibility(View.GONE);
		}

		String honour[] = doctorDetailsEntity.getHonour();
		if (honour != null && honour.length > 0) {
			ll_honour1.setVisibility(View.VISIBLE);

			for (int i = 0; i < honour.length; i++) {

				View contentView = getLayoutInflater().inflate(
						R.layout.view_num, null);

				ImageView iv_num = (ImageView) contentView
						.findViewById(R.id.iv_num);
				TextView tv_num = (TextView) contentView
						.findViewById(R.id.tv_num);
				iv_num.setImageResource(R.drawable.doctor_details_honor_number);
				tv_num.setText(honour[i]);
				ll_honour.addView(contentView);
			}

		} else {
			ll_honour1.setVisibility(View.GONE);
		}

		String reasons[] = doctorDetailsEntity.getReasons();
		if (reasons != null && reasons.length > 0) {
			rl_reason.setVisibility(View.VISIBLE);

			for (int i = 0; i < reasons.length; i++) {

				View contentView = getLayoutInflater().inflate(
						R.layout.view_num, null);

				ImageView iv_num = (ImageView) contentView
						.findViewById(R.id.iv_num);
				iv_num.setVisibility(View.GONE);
				// iv_num.setScaleType(ScaleType.CENTER);
				TextView tv_num = (TextView) contentView
						.findViewById(R.id.tv_num);
				iv_num.setImageResource(R.drawable.doctor_details_honor_number);
				tv_num.setText(reasons[i]);
				ll_reason.addView(contentView);
			}

		} else {
			rl_reason.setVisibility(View.GONE);
		}
	}

}
